import {
	orderCallbackApi,
} from '@/api/order.js'
import Md5 from 'js-md5'
// 调用微信支付
export function playOrder(data) {

	return new Promise((resolve, reject) => {

		// 支付成功回调
		function payCallbacky() {
			orderCallbackApi().then((res) => {			
			  	resolve()
			})

		}

		/* 随机数 */
		function randomString() {
			let chars = 'A2345678';
			let maxPos = chars.length;
			let pwd = '';
			for (let i = 0; i < 32; i++) {
				pwd += chars.charAt(Math.floor(Math.random() * maxPos));
			}
			return pwd;
		}

		// 前端签名
		function mixedencryMD5(data, randomString, timeStamp) {
			let pay = "appId=" + data.appid + "&nonceStr=" + randomString + "&package=prepay_id=" + data
				.prepay_id + "&signType=MD5" + "&timeStamp=" + timeStamp +
				"&key=7ae1526543474bba95ab996faefbc640";

			return Md5(pay);
		}
		
		// 时间戳
		function timeStamp() {
			return parseInt(new Date().getTime() / 1000) + ''
		}

		let random = randomString();
		//当前时间
		let time = timeStamp();
		//签名
		let parSigns = mixedencryMD5(data.payInfo, random, time);

		//调用微信官方支付接口弹出付款界面,输入密码扣款
		uni.requestPayment({
			timeStamp: time, //时间戳
			nonceStr: random, //随机字符串
			package: `prepay_id=${data.payInfo.prepay_id}`, //prepay_id
			signType: 'MD5', //签名算法MD5
			paySign: parSigns,
			success(res) {
				console.log(res)
				if (res.errMsg == "requestPayment:ok") {

					payCallbacky()

				} else {
					reject()
				}
			},
			fail(res) {
				reject()
			}
		})


	})






}